package interfacedesign.util;

import interfacedesign.bean.TokenInfo;
import lombok.val;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class RedisDB {
    private static Map<String, TokenInfo> redis = new HashMap<>();
    public static TokenInfo  get(String key) {
        TokenInfo tokenInfo = redis.get(key);
        if (null == tokenInfo || (tokenInfo.getExpireTime() != null && tokenInfo.getExpireTime() < System.currentTimeMillis())) {
            redis.remove(key);
            return null;
        }
        return tokenInfo;
    }
    public static TokenInfo put(String key, Object val) {
        put(key, val, new Date("2050-1-1").getTime());
        return redis.get(key);
    }
    public static Object put(String key, Object val, Long expireTime) {
        TokenInfo tokenInfo = (TokenInfo) val;
        tokenInfo.setExpireTime(expireTime + System.currentTimeMillis());
        return redis.put(key, tokenInfo);
    }
    public static boolean hasKey(String key) {

        return get(key) != null;
    }

}
